package  com.sat.common.widget

import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView

class OnSnapPageScrollListener(
    private val mHelper: PagerSnapHelper,
    private val mPageListener: OnPageChangedListener?
) :
    RecyclerView.OnScrollListener() {
    private var mLastPageIndex = 0
    override fun onScrollStateChanged(
        recyclerView: RecyclerView,
        newState: Int
    ) {
//        if(newState == RecyclerView.SCROLL_STATE_IDLE && mHelper!=null){
        val newPageIndex =
            mHelper.findTargetSnapPosition(recyclerView.layoutManager, 0, 0)
        if (newPageIndex != mLastPageIndex) {
            mLastPageIndex = newPageIndex
            mPageListener?.onPageChanged(mLastPageIndex, newPageIndex)
        }
        //        }
    }

    interface OnPageChangedListener {
        fun onPageChanged(oldPost: Int, newPos: Int)
    }

}
